package com.dongdongshop.controller;

import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @DubboReference
    private BrandService brandService;

    @RequestMapping("list")
    public String list() {
        return "/admin/brand";
    }

    @RequestMapping("brandList")
    @ResponseBody
    public Result brandList() {
        try {
            List<TbBrand> tbBrands = brandService.brandList();
            return Result.response(ResultEnum.SUCCESS).setData(tbBrands);
        } catch (Exception e) {

            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id) {
        TbBrand tbBrand = brandService.selectById(id);
        return Result.response(ResultEnum.SUCCESS).setData(tbBrand);
    }

    @RequestMapping("brandPage")
    @ResponseBody
    public Result brandPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize) {
        Page<TbBrand> page = brandService.brandPage(pageNum,pageSize);
        return Result.response(ResultEnum.SUCCESS).setData(page);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(@RequestParam("ids") Integer[] ids){
        int i = brandService.deleteByIds(ids);
        if (i <= 0){
            return Result.response(ResultEnum.ERROR);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbBrand tbBrand){
        int i = brandService.updatebyid(tbBrand);
        if (i <= 0){
            return Result.response(ResultEnum.ERROR);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbBrand tbBrand){
        int i = brandService.insertAll(tbBrand);
        if (i <= 0){
            return Result.response(ResultEnum.ERROR);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
}
